/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.gateway.dianlan.bits;

import cn.hutool.core.util.StrUtil;

/**
 * <b>(DianlanFireUnitFaultCodeBits)</b>
 * <p>
 * 故障码：
 * <p>
 * 0：无故障；
 * 一级故障（预警）：
 * 1: 4级预警；环境温度在90℃或烟雾、气体等参数标准
 * 2: 3级预警；环境温度在80℃或烟雾、气体等参数标准
 * 3: 2级预警；环境温度在75℃或烟雾、气体等参数标准
 * <p>
 * 二级故障：
 * 4：传感器故障；
 * 5：硬件故障；
 * 其中故障码为灭火器故障码
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/8/24
 */
public class DianlanFireUnitFaultCodeBits {

    public String parseBits(String result) {
        result = StrUtil.reverse(result);
        StringBuilder sb = new StringBuilder();

        // 截取7-8位，按二进制解析
        String faultLevel = StrUtil.sub(result, 6, 8);
        switch (faultLevel) {
            case "00":
                sb.append("无故障");
                break;
            case "01":
                sb.append("一级故障");
                break;
            case "10":
                sb.append("二级故障");
                break;
            default:
                break;
        }

        alarmLevel(StrUtil.sub(result, 0, 6), sb);

        return sb.toString();
    }

    private void alarmLevel(String result, StringBuilder sb) {
        sb.append("[");
        int code = Integer.parseInt(result, 2);
        switch (code) {
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                sb.append(code + "级火情");
                break;
            default:
                sb.append("正常");
        }
        sb.append("]");
    }

    public static void main(String[] args) {
        DianlanFireUnitFaultCodeBits cb     = new DianlanFireUnitFaultCodeBits();
        String                       result = cb.parseBits("01010000");
        System.out.println(result);
    }
}